//package com.alibaba.cirtus.ots.application.app.ability.facade;
//
//import com.alibaba.citrus.ots.sales.plan.repository.SalesPlanLineRepository;
//import com.alibaba.cz.base.tool.log.Log;
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
//import com.epoch.app.bcots.model.dto.*;
//
//import com.epoch.app.bcots.service.BaseDataService;
//import com.epoch.app.bcots.service.BcOtsService;
//import com.epoch.app.bcots.service.SalesPlanService;
//import com.epoch.app.otsapplicationappsdk.homepage.dto.RenderRequest;
//import com.epoch.app.otsapplicationappsdk.homepage.service.HomePageService;
//import com.epoch.app.otsapplicationappsdk.model.dto.QueryGlobalPermissionAllByUserResponse;
//import com.epoch.app.otsapplicationappsdk.service.OtsApplicationAppSdkService;
//import com.epoch.app.otsapplicationreportsdk.service.MonthSalesService;
//import com.epoch.app.otsforecastsales.model.dto.CountForecastWithAccessResponse;
//import com.epoch.app.otsforecastsales.model.dto.PageQueryForecastRequest;
//import com.epoch.app.otsforecastsales.otsforecastorderquery.service.OtsForecastOrderQueryService;
//import com.epoch.app.otsforecastsales.service.ApiService;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import javax.annotation.Resource;
//import java.text.SimpleDateFormat;
//import java.util.ArrayList;
//import java.util.Calendar;
//import java.util.Collections;
//import java.util.Date;
//import java.util.GregorianCalendar;
//import java.util.List;
//import java.util.Objects;
//
//@Service
//public class HomePageServiceImpl implements HomePageService {
//    private static Log log = Log.getLogger(HomePageServiceImpl.class);
//    @Resource
//    private SalesPlanLineRepository salesPlanLineRepository;
//
//    @Resource
//    SalesPlanService salesPlanService;
//
//    @Resource
//    BcOtsService bcOtsService;
//
//    @Resource
//    BaseDataService baseDataService;
//
//    @Resource
//    OtsForecastOrderQueryService otsForecastOrderQueryService;
//
//    @Resource
//    OtsApplicationAppSdkService  otsApplicationAppSdkService;
//
//    @Autowired
//    private ApiService apiService;
//
//    @Autowired
//    private MonthSalesService monthSalesService;
//
//
//    @Override
//    public Object render(RenderRequest renderRequest) {
//        JSONObject result = new JSONObject();
//        JSONObject data = new JSONObject();
//        try {
//            JSONObject jsonObject = new JSONObject();
//            //获取用户名字
//            LoginInformationResponse loginInformationResponse = bcOtsService.loginInformation();
//            System.out.println("首页请求获取用户相关信息展示结果 {} " + JSON.toJSONString(loginInformationResponse));
//            JSONObject userInfo = new JSONObject();
//            userInfo.put("userName", loginInformationResponse.getEmployeeName());
//
//            //获取用户角色
//            String employeeId = loginInformationResponse.getEmployeeId();
//            BaselineGetEmployeeRolesByIdRequest baselineGetEmployeeRolesByIdRequest = new BaselineGetEmployeeRolesByIdRequest();
//            baselineGetEmployeeRolesByIdRequest.setEmployeeId(employeeId);
//            BaselineGetEmployeeRolesByIdResponse baselineGetEmployeeRolesByIdResponse = baseDataService.baselineGetEmployeeRolesById(baselineGetEmployeeRolesByIdRequest);
//            String result1 = JSON.toJSONString(baselineGetEmployeeRolesByIdResponse);
//            JSONObject listResult = JSON.parseObject(result1);
//            JSONArray result3 = listResult.getJSONArray("result");
//            userInfo.put("jobText", listResult.getJSONArray("result"));
//            JSONArray jsonArray = userInfo.getJSONArray("jobText");
//            // JSONObject userInfo = result1.getJSONObject("userInfo");
//
//            //根据用户角色获取用户权限点
//            QueryGlobalPermissionAllByUserResponse queryGlobalPermissionAllByUserResponse = otsApplicationAppSdkService.queryGlobalPermissionAllByUser();
//            userInfo.put("plan",queryGlobalPermissionAllByUserResponse.getResult());
//
//
//            //根据关联id获取组织
//            if (StringUtils.isNotBlank(loginInformationResponse.getRelatedOrganizationId())) {
//                String relatedOrganizationId = loginInformationResponse.getRelatedOrganizationId();
//                JSONObject organizationSDO =getOranziation(loginInformationResponse);
//                log.info("首页获取关联组织信息 {} ", JSON.toJSONString(organizationSDO));
//                String name = organizationSDO.getString("name");
//                userInfo.put("area", name);
//            }
//            data.put("userInfo",userInfo);
//
//            //获取预报订单金额
//            PageQueryForecastRequest pageQueryForecastRequest = new PageQueryForecastRequest();
//           //、 pageQueryForecastRequest.setCreatorId(loginInformationResponse.getEmployeeId());
//
//
//            List<String> gmtCreate = new ArrayList<>();
//            Calendar calendar = new GregorianCalendar();
//            calendar.add(Calendar.DAY_OF_MONTH,0);
//            calendar.set(Calendar.HOUR_OF_DAY,0);
//            calendar.set(Calendar.MINUTE,0);
//            calendar.set(Calendar.SECOND,0);
//           // calendar.set(Calendar.MILLISECOND,0);
//            Date dayStart = calendar.getTime();
//            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            String startStr = simpleDateFormat.format(dayStart);
//
//            //一天的结束时间 yyyy:MM:dd 23:59:59
//            calendar.set(Calendar.HOUR_OF_DAY,23);
//            calendar.set(Calendar.MINUTE,59);
//            calendar.set(Calendar.SECOND,59);
//           // calendar.set(Calendar.MILLISECOND,999);
//            Date dayEnd = calendar.getTime();
//            String endStr = simpleDateFormat.format(dayEnd);
//
//            gmtCreate.add(startStr);
//            gmtCreate.add(endStr );
//            pageQueryForecastRequest.setGmtCreate(gmtCreate);
//            //Result<Long> planCount = otsForecastOrderQueryService.countForecastOrder(pageQueryForecastRequest);
//            CountForecastWithAccessResponse planCount = apiService.countForecastWithAccess(pageQueryForecastRequest);
//
//            //PageQueryForecastRequest request = new PageQueryForecastRequest();
//            pageQueryForecastRequest.setMainStatusList(Collections.singletonList(2));
//           // Result<Long> planCountToday = otsForecastOrderQueryService.countForecastOrder(pageQueryForecastRequest);
//            CountForecastWithAccessResponse planCountToday = apiService.countForecastWithAccess(pageQueryForecastRequest);
//            JSONObject planOrder = new JSONObject();
//            planOrder.put("planCount",planCount.getResult());
//            planOrder.put("planCountToday",planCountToday.getResult());
//            data.put("planOrder",planOrder);
//
//
//            //获取月度计划计划金额
////            if (StringUtils.isNotBlank(loginInformationResponse.getRelatedOrganizationType())&&StringUtils.isNotBlank(loginInformationResponse.getRelatedOrganizationId())) {
////                PageQueryPlanScItemListRequest pageQueryPlanScItemListRequest = new PageQueryPlanScItemListRequest();
////                String planId = null;
////                try {
////                    planId = String.valueOf(getPlanId(loginInformationResponse));
////                    if (StringUtils.isNotEmpty(planId)){
////                        pageQueryPlanScItemListRequest.setPlanId(planId);
////                    }
////                } catch (Exception e) {
////                    log.info("暂时查询不到月度计划id{}", e.getMessage());
////                }
////                pageQueryPlanScItemListRequest.setBizType(Integer.valueOf(loginInformationResponse.getRelatedOrganizationType()));
////                pageQueryPlanScItemListRequest.setBizId(loginInformationResponse.getRelatedOrganizationId());
////                pageQueryPlanScItemListRequest.setPoolType(3);
////                log.info("首页获取月度计划列表查询个人参数{} ", JSON.toJSONString(pageQueryPlanScItemListRequest));
////                JSONObject themle = new JSONObject();
////                PageQueryPlanScItemListResponse pageQueryPlanScItemListResponse = null;
////                try {
////                    pageQueryPlanScItemListResponse = salesPlanService.pageQueryPlanScItemList(pageQueryPlanScItemListRequest);
////                    log.info("首页获取月度计划个人列表相关信息{} ", JSON.toJSONString(pageQueryPlanScItemListResponse));
////                    String result4 = JSON.toJSONString(pageQueryPlanScItemListResponse);
////                    JSONObject listresult1 = JSON.parseObject(result4);
////                    JSONArray resultJson2 = listresult1.getJSONArray("result");
////                    if (resultJson2 !=null&&resultJson2.size()>1) {
////                        String monthMoney = Objects.toString(resultJson2.getJSONObject(0).get("monthMoney"));
////                        log.info("首页获取月度计划计划金额{} ", JSON.toJSONString(monthMoney));
////                        String monthCount = Objects.toString(resultJson2.getJSONObject(0).get("monthCount"));
////                        log.info("首页获取月度计划计划量{} ", JSON.toJSONString(monthCount));
////                        themle.put("monthMoney", monthMoney);
////                        themle.put("monthCount", monthCount);
////                    }
////                } catch (Exception e) {
////                    log.error("暂时查询不到月度计划计划金额和计划量{}", e);
////                }
////                data.put("themList", themle);
////                }
//
//            //获取销售进度看板
//            try {
//                Object salesKanbanObj = monthSalesService.getMonthSalesAppView();
//                log.info("首页销售进度看板结果 {} ", JSON.toJSONString(salesKanbanObj));
//                data.put("salesKanban",salesKanbanObj);
//            } catch (Exception e) {
//                log.error("C端首页获取销售进度异常");
//                e.printStackTrace();
//            }
//
//
//            result.put("success", true);
//            result.put("result", data);
//            log.info("首页获取月度计划最终结果展示{} ", JSON.toJSONString(result));
//
//        } catch (Exception e) {
//            log.error("首页渲染异常 ", e.getMessage());
//            result.put("success", false);
//            result.put("message", "首页渲染异常");
//        }
//        return result;
//    }
//
//           private Long getStartTime() {
//                 Calendar todayStart = Calendar.getInstance();
//                 todayStart.set(Calendar.HOUR, 0);
//                 todayStart.set(Calendar.MINUTE, 0);
//                  todayStart.set(Calendar.SECOND, 0);
//                 todayStart.set(Calendar.MILLISECOND, 0);
//                 return todayStart.getTime().getTime();
//             }
//
//             private Long getEndTime() {
//                 Calendar todayEnd = Calendar.getInstance();
//                 todayEnd.set(Calendar.HOUR, 23);
//                 todayEnd.set(Calendar.MINUTE, 59);
//                todayEnd.set(Calendar.SECOND, 59);
//                 todayEnd.set(Calendar.MILLISECOND, 999);
//                 return todayEnd.getTime().getTime();
//             }
//
//    private String  getPlanId(LoginInformationResponse loginInformationResponse){
//        PageQuerySalePlanListRequest pageQuerySalePlanListRequest = new PageQuerySalePlanListRequest();
//        Long id=null;
//        Calendar now = Calendar.getInstance();
//        Date date = new Date();
//        //设置要获取到什么样的时间
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
//        //获取String类型的时间
//        String plantime = sdf.format(date);
//        pageQuerySalePlanListRequest.setPlanTime(plantime);
//
//        JSONObject organizationSDO =getOranziation(loginInformationResponse);
//        pageQuerySalePlanListRequest.setOrgBusinessUnitCode(organizationSDO.getString("code"));
//        log.info("首页获取月度计划id入参展示{} ", JSON.toJSONString(pageQuerySalePlanListRequest));
//        try {
//            PageQuerySalePlanListResponse pageQuerySalePlanListResponse = salesPlanService.pageQuerySalePlanList(pageQuerySalePlanListRequest);
//            if(Objects.nonNull(pageQuerySalePlanListResponse)){
//                String result = JSON.toJSONString(pageQuerySalePlanListResponse);
//                JSONObject listresult = JSON.parseObject(result);
//                JSONArray  list  = listresult.getJSONArray("result");
//                String  planId = String.valueOf(list.getJSONObject(0).get("id"));
//                return planId;
//            }
//        } catch (Exception e) {
//            log.error("获取月度计划planId失败", e);
//        }
//        return null;
//    }
//
//    private JSONObject getOranziation(LoginInformationResponse loginInformationResponse) {
//        // if (StringUtils.isNotBlank(loginInformationResponse.getRelatedOrganizationId())) {
//        String relatedOrganizationId = loginInformationResponse.getRelatedOrganizationId();
//        BaselineGetOrganizationByIdRequest baselineGetOrganizationByIdRequest = new BaselineGetOrganizationByIdRequest();
//        baselineGetOrganizationByIdRequest.setId(relatedOrganizationId);
//        Object sdo = baseDataService.baselineGetOrganizationById(baselineGetOrganizationByIdRequest);
//        String result1 = JSON.toJSONString(sdo);//map转String
//        JSONObject listresult = JSON.parseObject(result1);
//        JSONObject resultJson = listresult.getJSONObject("result");
//        JSONObject resultJson1 = resultJson.getJSONObject("result");
//        JSONObject organizationSDO = resultJson1.getJSONObject("organizationSDO");
//        return organizationSDO;
//    }
//}
//
